<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/CustomCommand.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/DaoFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';
	
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Employee.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/JobHistory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Phone.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Place.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/EmployeeAddress.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	/*
     * Command to create an employee
	 */
	class CommandCreateEmployee extends CustomCommand {
		/*
		 * Constructor of the class
		 */
		public function __construct($ci, $firstName, $secondName, $firstLastName, 
			$secondLastName, $emailAddress, $addressStartDate, $addressEndDate, $country, 
			$stateProvince, $city, $zone, $homePhone, $mobilePhone, $workPhone, $faxPhone, 
			$startDate, $endDate, $job, $salary, $branchRif, $departmentId) {
			/**
			 * Creating an employee
			 */
			$this -> employee = new Employee($ci, $firstName, $secondName, $firstLastName, 
				$secondLastName, $emailAddress);
				
			/**
			 * Creating a job history
			 */
			$this -> jobHistory = new JobHistory($startDate, $endDate, $job, $salary, $ci, 
				$departmentId);
				
			/**
			 * Creating a home phone
			 */
			$homePhone = explode('.', $homePhone); 
			$homePhoneSerial = $homePhone[0];
			$homePhoneNumber = $homePhone[1];
			$this -> homePhone = new Phone('HABITACION', $homePhoneSerial, $homePhoneNumber, 
				$ci, 'EMPLOYEE');
			
			/**
			 * Creating a mobile phone
			 */
			$mobilePhone = explode('.', $mobilePhone); 
			$mobilePhoneSerial = $mobilePhone[0];
			$mobilePhoneNumber = $mobilePhone[1];
			$this -> mobilePhone = new Phone('MOVIL', $mobilePhoneSerial, $mobilePhoneNumber, 
				$ci, 'EMPLOYEE');
				
			/**
			 * Creating a work phone
			 */
			$workPhone = explode('.', $workPhone); 
			$workPhoneSerial = $workPhone[0];
			$workPhoneNumber = $workPhone[1];
			$this -> workPhone = new Phone('TRABAJO', $workPhoneSerial, $workPhoneNumber, 
				$ci, 'EMPLOYEE');
			
			/**
			 * Creating a fax phone
			 */
			$faxPhone = explode('.', $faxPhone); 
			$faxPhoneSerial = $faxPhone[0];
			$faxPhoneNumber = $faxPhone[1];
			$this -> faxPhone = new Phone('FAX', $faxPhoneSerial, $faxPhoneNumber, 
				$ci, 'EMPLOYEE');
				
			/**
			 * Creating a place
			 */
			$this -> country = new Place("PAIS", $country);
			$this -> stateProvince = new Place("ESTADO/PROVINCIA", $stateProvince);
			$this -> city = new Place("CIUDAD", $city);
			$this -> zone = new Place("SECTOR", $zone);
			
			/**
			 * Creating an employee address
			 */
			$this -> employeeAddress = new EmployeeAddress($addressStartDate, $addressEndDate, 
				$ci);	
		}
		
		/*
		* Method to execute to create an employee
		*/
		public function execute() {
			/**
			 * Creating an employee
			 */			 
			$daoFactory = DaoFactory :: getMySqlDaoFactory();
			$iead = $daoFactory -> getMySqlEmployeeAccessDao();		
			$employeeCreation = $iead -> createEmployee($this -> employee);
			
			/**
			 * Creating a job history
			 */
			$command = CommandFactory :: createCommandCreateJobHistory
				($this -> jobHistory -> getPkStartDate(), $this -> jobHistory -> getEndDate(), 
				$this -> jobHistory -> getJob(), $this -> jobHistory -> getSalary(), 
				$this -> jobHistory -> getPkFkIdNumberEmployee(), 
				$this -> jobHistory -> getPkFkIdDepartment());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a home phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> homePhone -> getType(),
				$this -> homePhone -> getSerial(), $this -> homePhone -> getNumber(), 
				$this -> homePhone -> getIdOwner(), $this -> homePhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a mobile phone
			 */
			$command = CommandFactory :: createCommandCreatePhone
				($this -> mobilePhone -> getType(), $this -> mobilePhone -> getSerial(), 
				$this -> mobilePhone -> getNumber(), $this -> mobilePhone -> getIdOwner(), 
				$this -> mobilePhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a work phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> workPhone -> getType(),
				$this -> workPhone -> getSerial(), $this -> workPhone -> getNumber(), 
				$this -> workPhone -> getIdOwner(), $this -> workPhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a fax phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> faxPhone -> getType(),
				$this -> faxPhone -> getSerial(), $this -> faxPhone -> getNumber(), 
				$this -> faxPhone -> getIdOwner(), $this -> faxPhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a country
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> country -> getType(), 
				$this -> country -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a state/province
			 */
			$command = CommandFactory :: createCommandCreatePlace
				($this -> stateProvince -> getType(), $this -> stateProvince -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a city
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> city -> getType(), 
				$this -> city -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a zone
			 */
			$command = CommandFactory :: createCommandCreatePlace($this -> zone -> getType(), 
				$this -> zone -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating an employee address
			 */
			$command = CommandFactory :: createCommandCreateEmployeeAddress
				($this -> employeeAddress -> getPkStartDate(), 
				$this -> employeeAddress -> getEndDate(), 
				$this -> employeeAddress -> getPkFkIdNumberEmployee());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			$this -> result = $employeeCreation;
		}
	}
?>